package com.dtw.ditouwang.ui.mine;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.RadioButton;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.ReportBean;
import com.dtw.ditouwang.databinding.ActivityReportBinding;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseActivity;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReportActivity extends BaseActivity implements View.OnClickListener {
    RadioButton month, walk;
    BarChart barChart;
    ActivityReportBinding activityReportBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityReportBinding = DataBindingUtil.setContentView(this, R.layout.activity_report);
        findViewById(R.id.radio_run).setOnClickListener(this);
        findViewById(R.id.radio_week).setOnClickListener(this);
        month = findViewById(R.id.radio_month);
        month.setOnClickListener(this);
        walk = findViewById(R.id.radio_walk);
        walk.setOnClickListener(this);
        barChart = findViewById(R.id.barchart);
        initData();
    }

    @Override
    public void onClick(View view) {
        initData();
    }

    private void initData() {
        int type = 2, date = 1;
        if (month.isChecked()) {
            date = 2;
        }
        if (walk.isChecked()) {
            type = 1;
        }
        webApi.getReport(UserRepository.getInstance().getUserID(), type, date).enqueue(new Callback<ReportBean>() {
            @Override
            public void onResponse(Call<ReportBean> call, Response<ReportBean> response) {
                if (response.body().isResult()) {
                    activityReportBinding.setReport(response.body().getData().getStatistical());
                    initChart(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<ReportBean> call, Throwable t) {

            }
        });
    }

    private void initChart(final List<ReportBean.DataBean.ListBean> listBeanList) {
        ArrayList<BarEntry> entries = new ArrayList<>();
        for (int i = 0; i < listBeanList.size(); i++) {
            BarEntry barEntry = new BarEntry(i, (float) listBeanList.get(i).getTotal());
            entries.add(barEntry);
        }
        BarDataSet barDataSet = new BarDataSet(entries, "");
        barChart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return listBeanList.get((int) value).getDate();
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });
        barDataSet.setBarBorderColor(ContextCompat.getColor(this, R.color.commen_green));
        BarData data = new BarData(barDataSet);
        data.setBarWidth(0.1f);
        barChart.getDescription().setEnabled(false);
        barChart.setData(data);
        barChart.getAxisLeft().setDrawGridLines(false);
        barChart.getAxisLeft().setAxisMinimum(0);
        barChart.getAxisRight().setEnabled(false);
        barChart.getXAxis().setDrawGridLines(false);
        barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        barChart.setDoubleTapToZoomEnabled(false);
        barChart.setScaleEnabled(false);
        barChart.invalidate();
    }
}
